<!--
+----------------------------------------------------------------------
| 友得云客  - 开启房产营销新纪元
+----------------------------------------------------------------------
| Copyright (c) 2019~2023 优得（西安）信息科技有限公司版权所有
+----------------------------------------------------------------------
| Licensed 友得云客不是自由软件 未经允许不可移除相关版权
+----------------------------------------------------------------------
| Author: www.youdeyunke.com
+----------------------------------------------------------------------
-->
<template>
  <div class="vr-form" v-cloak v-if="item">
    <el-form label-position="right" size="small" label-width="120px" v-loading="loading">
      <el-form-item label="名称" required>
        <el-input style="width: 300px" v-model="item.name" placeholder="请输入姓名"></el-input>
      </el-form-item>

      <el-form-item label="电话号码" required>
        <el-input style="width: 300px" v-model="item.mobile" placeholder="请输入电话号码"></el-input>
      </el-form-item>

      <el-form-item label="是否启用" required>
        <el-radio-group v-model="item.enable">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item size="large">
        <el-button size="mini" type="default" @click="$emit('cancle')">取消</el-button>
        <el-button size="mini" type="primary" @click="submitHandle">提交保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped></style>

<script>
import { createPostServer, updatePostServer } from "@/api/post-server"
export default {

  data() {
    return {
      loading: false,
    };
  },

  props: {
    item: { type: Object },
  },

  mounted: function () { },

  computed: {
    isNew: function () {
      return !this.item.id;
    },
  },

  methods: {
    doCreate: function () {
      this.loading = true;
      createPostServer(this.item).then((resp) => {
        this.loading = false;
        if (resp.code != 0) {
          return
        }
        this.$message.success("已保存");
        this.$emit("change", {});
      });
    },
    doUpdate: function () {
      this.loading = true;
      updatePostServer(this.item).then((resp) => {
        this.loading = false;
        if (resp.code != 0) {
          return
        }
        this.$message.success("已保存");
        this.$emit("change", {});
      });
    },
    validate: function () {
      // validate
      var item = this.item;
      console.log("validate item is ", item);

      if (!item.name) {
        this.$message.error("请输入姓名");
        return false;
      }
      if (!item.post_id) {
        this.$message.error("请选择厂房");
        return false;
      }
      if (!item.mobile) {
        this.$message.error("请输入电话号码");
        return false;
      }
      return true;
    },
    submitHandle: function () {
      var isok = this.validate();
      if (!isok) {
        return;
      }

      console.log("submit item", this.item, "isok", isok);
      if (!this.isNew) {
        this.doUpdate();
        return;
      }
      this.doCreate();
    },
  }
}
</script>
